﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class EventLine : MonoBehaviour {
	
	[HideInInspector]
	public int Player;
	[HideInInspector]
	public GameObject Starts;
	[HideInInspector]
	public GameObject Target;
	[HideInInspector]
	public Finder.FindType type;
	public float proceed;
	[HideInInspector]
	public float Proceed_Total;
	 float ProceedSpeed;

	float Percentage;
	void Update () {
		proceed -= 1f * ProceedSpeed;
		if (proceed < 0) 
		{
			proceed = 0;
			switch (type) 
			{
			case Finder.FindType.explore:
				OwnerJudge ();
				break;			
			case Finder.FindType.colony:
				OwnerJudge ();
				Colonize ();
				break;			
			case Finder.FindType.enemy:
				Doom ();
				break;
			default:
				break;
			}
			Destroy (gameObject);
		}//起点被毁
		else if (Starts.GetComponent<StarSystem> ().State_ == StarSystem.States.destroyed || Starts.GetComponent<StarSystem> ().Owner == 0) 
		{
			if(type!=Finder.FindType.enemy)
				Destroy (gameObject);
		}

		Percentage = (int)((proceed / Proceed_Total) * 100);
		Proceedure.text = Percentage.ToString();
	}
	void OwnerJudge()
	{ 
		if(Target.GetComponent<StarSystem> ().Owner == 0)//无人殖民，显示探索
		{	
			Target.GetComponent<StarSystem> ().VisibleState [Player-1].VisibleType = StarSystem.Visible.VisibleTypes.Explored;
			Target.GetComponent<StarSystem> ().VisibleState[Player-1].ExplorCD = ControlCenter.Instance.speedAndTime.ExploreCD;
		}
		else if (Target.GetComponent<StarSystem> ().Owner != Player) //有人殖民
		{
			Target.GetComponent<StarSystem> ().VisibleState [Player-1].VisibleType = StarSystem.Visible.VisibleTypes.Uncomunicate;
			Target.GetComponent<StarSystem> ().VisibleState [Player-1].ComunicateCD=ControlCenter.Instance.speedAndTime.ComubicatedCD;
			Starts.GetComponent<StarSystem>().VisibleState[Target.GetComponent<StarSystem> ().Owner-1].ComunicateCD=ControlCenter.Instance.speedAndTime.ComubicatedCD;
			ControlCenter.Instance.Players [Player-1].GetComponent<Player> ().CurState.EnemySystemCount += 1;
		}
	}
	void Colonize()//殖民
	{		
		if (Target.GetComponent<StarSystem> ().Owner == 0 && Target.GetComponent<StarSystem> ().State_ == StarSystem.States.origin) 
		{
			Target.GetComponent<StarSystem> ().VisibleState [Player-1].VisibleType = StarSystem.Visible.VisibleTypes.Owend;
			Target.GetComponent<StarSystem> ().State_ = StarSystem.States.colonized;
			Target.GetComponent<StarSystem> ().Owner = Player;
			ControlCenter.Instance.Players [Target.GetComponent<StarSystem> ().Owner-1].GetComponent<Player> ().CurState.OwnedSystems += 1;
		}
	}
	void Doom()//毁灭
	{
		Target.GetComponent<StarSystem> ().State_ = StarSystem.States.destroyed;
		Target.GetComponent<StarSystem> ().VisibleState [Player-1].VisibleType = StarSystem.Visible.VisibleTypes.Explored;
		if (Target.GetComponent<StarSystem> ().Owner != 0) {
			ControlCenter.Instance.Players [Target.GetComponent<StarSystem> ().Owner-1].GetComponent<Player> ().CurState.OwnedSystems -= 1;
			if(Target.GetComponent<StarSystem> ().VisibleState [Player-1].VisibleType != StarSystem.Visible.VisibleTypes.Unexplore)
				ControlCenter.Instance.Players [Player-1].GetComponent<Player> ().CurState.EnemySystemCount -= 1;
		}
		Target.GetComponent<StarSystem> ().Owner = 0;
	}


	//初始化设置
	Vector3 StartPoint;
	Vector3 EndPoint;
	TextMesh Proceedure;
	void Start ()
	{
		StartPoint = Starts.transform.position;
		EndPoint = Target.transform.position;
		Vector3[] Vects = { StartPoint, EndPoint };
		GetComponent<LineRenderer> ().SetPositions (Vects);

		Proceedure = transform.Find ("Proceed").GetComponent<TextMesh> ();
		Vector3 Proc = (StartPoint + EndPoint) / 2 + new Vector3 (0.5f, -0, 5f);
		Proceedure.transform.position = Proc;

		switch (type) {
		case Finder.FindType.explore:
			GetComponent<LineRenderer> ().endColor = new Color (0, 1, 1);
			ProceedSpeed = ControlCenter.Instance.actionSpeed.Explore;
			break;			
		case Finder.FindType.colony:
			GetComponent<LineRenderer> ().endColor = new Color (0, 1, 0);
			ProceedSpeed = ControlCenter.Instance.actionSpeed.Colony;
			break;			
		case Finder.FindType.enemy:
			GetComponent<LineRenderer> ().endColor = new Color (1, 0, 0);
			ProceedSpeed = ControlCenter.Instance.actionSpeed.Destroy;
			break;
		default:
			break;
		}
		ProceedSpeed *= (ControlCenter.Instance.actionSpeed.Base);
		GetComponent<LineRenderer> ().startColor = new Color (1, 1, 1);

		proceed = Proceed_Total*ControlCenter.Instance.YearRate;//时间修正
	
	}
}
